package com.xjz.controller;

import com.xjz.common.BaseResponse;
import com.xjz.common.ResultUtils;
import com.xjz.common.enums.ErrorCode;
import com.xjz.exception.BusinessException;
import com.xjz.model.dto.postthumb.PostThumbAddRequest;
import com.xjz.model.entity.User;
import com.xjz.service.PostThumbService;
import com.xjz.service.UserService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * ClassName: PostThumbController
 * Package: com.xjz.controller
 * Description:
 *  帖子点赞接口
 * @Author 许小健
 * @Create 2025/8/11 22:09
 * @Version 1.0
 */

@RestController
@RequestMapping("/post_thumb")
@Slf4j
public class PostThumbController {

    @Resource
    private PostThumbService postThumbService;

    @Resource
    private UserService userService;

    @PostMapping("/")
    public BaseResponse<Integer> doThumb(@RequestBody PostThumbAddRequest postThumbAddRequest,
                                         HttpServletRequest request) {
        if (postThumbAddRequest == null || postThumbAddRequest.getPostId() <= 0) {
            throw new BusinessException(ErrorCode.PARAMS_ERROR);
        }

        //登录才能点赞
        User loginUser = userService.getLoginUser(request);
        Long postId = postThumbAddRequest.getPostId();
        Integer result = postThumbService.doPostThumb(postId,loginUser);
        return ResultUtils.success(result);
    }
}
